#ifndef LISTWIDGETSHELL_H
#define LISTWIDGETSHELL_H

#include <QObject>
#include "../../XXqtDefine.h"
#include <QVariant>
#include <QListWidget>
#include "../Cell/CellBase.h"

class ListWidgetShell : public QObject
{
    Q_OBJECT
public:
    explicit ListWidgetShell(QObject *parent = nullptr);
    virtual ~ListWidgetShell() {}

    void shell(QListWidget *listWidget);

    // config
    void configRow(QMetaObject cls, QSize size);
    void configDblSelectable(bool selectable);

    // action
    void appendData(const QVariant &data);
    void insertData(int row, const QVariant &data);
    void removeData(int row);
    QVariantList getData();
    QVariant getData(int row);
    QVariant takeData(int row);

    void setData(const QVariantList &datas);
    void setData(int row, const QVariant &data);
    void updateData(int row, const QVariant &data);
    void select(int row);
    int findRowEqualTo(const QVariant &data);

signals:
    void sigEvent(int row, QString name, QVariant info);
    void sigDataChanged(int row);
    void sigSelectionChanged(int current, int previous);

private:
    void setCurrentActive(CellBase *cell);
    void setCurrentSelect(CellBase *cell);
    void makeItemCell(QListWidgetItem **item, CellBase **cell, QVariant data);

public slots:
    void listWidget_onItemClicked(QListWidgetItem *item);
    void listWidget_onItemDoubleClicked(QListWidgetItem *item);
    void xcell_onEvent(QString name, QVariant info);
    void xcell_onDataChanged();
    void xcell_onDestroyed(QObject *object);

private:
    QListWidget *_listWidget;
    QMetaObject _cls;
    QSize _rowSize;
    CellBase *_currentSelectCell;
    CellBase *_currentActiveCell;
    QList<CellBase*> _cells;
};

#endif // LISTWIDGETSHELL_H
